package com.kk.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kk.dao.UserDao;
import com.kk.po.User;
import java.util.Collections;
import javax.annotation.Resource;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsPasswordService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.provisioning.UserDetailsManager;

/**
 * <p>
 * .
 *
 * @author o_o
 * @date 2025-09-07
 */
@Configuration
public class DBUserDetailsManager implements UserDetailsManager, UserDetailsPasswordService {

    @Resource
    private UserDao userDao;

    @Override
    public UserDetails updatePassword(UserDetails userDetails, String s) {
        return null;
    }

    @Override
    public void createUser(UserDetails userDetails) {

    }

    @Override
    public void updateUser(UserDetails userDetails) {

    }

    @Override
    public void deleteUser(String s) {

    }

    @Override
    public void changePassword(String s, String s1) {

    }

    @Override
    public boolean userExists(String s) {
        return false;
    }

    /**
     * 从数据库获取用户信息
     *
     * @param s
     * @return
     * @throws UsernameNotFoundException
     */
    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.eq("username", s);
        User user = userDao.selectOne(userQueryWrapper);

        org.springframework.security.core.userdetails.User user1 = new org.springframework.security.core.userdetails.User(
                s, user.getPassword(), user.isEnable(),
                true,
                true,
                true,
                Collections.emptyList() //用户账号、用户凭证、用户不被锁定， 权限列表
        );

        return user1;
    }
}
